package cn.gqr.controller;

import cn.gqr.entity.Book;
import cn.gqr.entity.Type;
import cn.gqr.service.BookService;
import cn.gqr.service.TypeService;
import cn.gqr.tool.RespJson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

@RestController
@RequestMapping("/api/adm")
public class ProductAdminController {

    @Autowired
    private BookService bookservice;

    @Autowired
    private TypeService typeservice;


    //书本的添加
    @PreAuthorize("hasAuthority('adm:insertBook')")
    @RequestMapping("/book/insert")
    public Map<String,Object> insert(Book book, @RequestParam(value = "pic",required = false) MultipartFile file){
        boolean b = bookservice.insertBook(book, file);
        return RespJson.pack(200,b?"添加成功":"添加失败",null);
    }

    //书本的删除
    @PreAuthorize("hasAuthority('adm:deleteBook')")
    @RequestMapping("/book/delete/{id}")
    public Map<String,Object> delBookById(@PathVariable("id") Integer id){
        boolean b = bookservice.delBookById(id);
        return RespJson.pack(200,b?"删除成功":"删除失败",null);
    }

    //书本的修改
    @PreAuthorize("hasAuthority('adm:updateBook')")
    @RequestMapping("/book/update")
    public Map<String,Object> update(Book book,@RequestParam(value = "pic",required = false) MultipartFile file){
        boolean b = bookservice.updateBook(book, file);
        return RespJson.pack(200,b?"修改成功":"修改失败",null);
    }

    //类型的添加
    @PreAuthorize("hasAuthority('adm:insertType')")
    @RequestMapping("/type/insert")
    public Map<String,Object> insert(Type type){
        boolean b = typeservice.insertType(type);
        return RespJson.pack(200,b?"添加成功":"添加失败",null);
    }

    //类型的修改
    @PreAuthorize("hasAuthority('adm:updateType')")
    @RequestMapping("/type/update")
    public Map<String,Object> update(Type type){
        boolean b = typeservice.updateType(type);
        return RespJson.pack(200,b?"修改成功":"修改失败",null);
    }

    //类型的删除
    @PreAuthorize("hasAuthority('adm:deleteType')")
    @RequestMapping("/type/delete/{id}")
    public Map<String,Object> delTypeById(@PathVariable("id") Integer id){
        boolean b = typeservice.deleteType(id);
        return RespJson.pack(200,b?"删除成功":"删除失败",null);
    }


}
